package club.terroir.client.producteur;

import java.util.Comparator;
import java.util.List;

import club.terroir.client.ControlerClubAdmin;
import club.terroir.client.Style;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;

public class AchatsListGUI extends VerticalPanel {

	private Contact contactInfo;
	private Label labelTitre = new Label(
			"Détail des commandes et achats d'un client");
	// Create a data provider.
	ListDataProvider<Achat> dataProvider = new ListDataProvider<Achat>();
	static public AchatsListGUI instance = new AchatsListGUI();

	private AchatsListGUI() {
		super();
		labelTitre.setStyleName(Style.Titre);
		this.add(labelTitre);

		CellTable<Achat> table = initTable();
		SimplePager pager = new SimplePager();
		// Set the cellList as the display.
		pager.setDisplay(table);
		this.add(labelTitre);
		this.add(table);
		this.add(pager);
	}

	

	private Button initBurronNouvelAchat() {
		Button buttonNouvelAchat = new Button("Nouvel achat");
		buttonNouvelAchat.setWidth("150");
		buttonNouvelAchat.setTitle("Nouveau Client");
		buttonNouvelAchat.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ControlerClubAdmin.instance.displayNouvelAchat(contactInfo);
			}
		});
		return buttonNouvelAchat;
	}


	public Contact getContactInfo() {
		return contactInfo;
	}

	public void setContactInfo(Contact contactInfo) {
		this.contactInfo = contactInfo;
		
		//this.labelTitre.setText("Détail des commandes et achats de :"+ contactInfo.name);
		this.setListAchat();

	}

	private CellTable<Achat> initTable() {

		// Create a CellTable.
		CellTable<Achat> table = new CellTable<Achat>();

		// Create name column.
		TextColumn<Achat> dateColumn = new TextColumn<Achat>() {
			@Override
			public String getValue(Achat achat) {
				return "" + achat.getDateStr();
			}
		};

		// Make the name column sortable.
		dateColumn.setSortable(true);

		// Create name column.
		TextColumn<Achat> achatColumn = new TextColumn<Achat>() {
			@Override
			public String getValue(Achat achat) {
				return achat.label;
			}
		};

		// Make the name column sortable.
		achatColumn.setSortable(true);

		// Create address column.
		TextColumn<Achat> commentColumn = new TextColumn<Achat>() {
			@Override
			public String getValue(Achat achat) {
				return achat.comment;
			}
		};
		commentColumn.setSortable(true);

		FieldUpdater<Achat, String> fieldUpdaterButton = new FieldUpdater<Achat, String>() {
			@Override
			public void update(int index, Achat achat, String value) {
				Window.alert("Ouep " + achat);
			}
		};
		Column<Achat, String> buttonColumn = new Column<Achat, String>(
				new ButtonCell()) {
			@Override
			public String getValue(Achat object) {
				return " ";
			}
		};
		buttonColumn.setFieldUpdater(fieldUpdaterButton);

		// Add the columns.
		table.addColumn(dateColumn, "Date");
		table.addColumn(achatColumn, "Achat");
		table.addColumn(commentColumn, "Commentaire");
		table.addColumn(buttonColumn, " ");

		// Connect the table to the data provider.
		dataProvider.addDataDisplay(table);

		// Add the data to the data provider, which automatically pushes it to
		// the
		// widget.
		List<Achat> list = dataProvider.getList();
		//
		ListHandler<Achat> columnSortHandlerByDate = new ListHandler<Achat>(
				list);
		columnSortHandlerByDate.setComparator(dateColumn,
				new Comparator<Achat>() {
					public int compare(Achat o1, Achat o2) {
						if (o1 == o2) {
							return 0;
						}

						// Compare the name columns.
						if (o1 != null) {
							return (o2 != null) ? o1.date.compareTo(o2.date)
									: 1;
						}
						return -1;
					}
				});

		table.addColumnSortHandler(columnSortHandlerByDate);

		// We know that the data is sorted alphabetically by default.
		table.getColumnSortList().push(dateColumn);

		// Add a ColumnSortEvent.ListHandler to connect sorting to the
		// java.util.List.
		ListHandler<Achat> columnSortHandlerByName = new ListHandler<Achat>(
				list);
		columnSortHandlerByName.setComparator(achatColumn,
				new Comparator<Achat>() {
					public int compare(Achat o1, Achat o2) {
						if (o1 == o2) {
							return 0;
						}

						// Compare the name columns.
						if (o1 != null) {
							return (o2 != null) ? o1.label.compareTo(o2.label)
									: 1;
						}
						return -1;
					}
				});

		table.addColumnSortHandler(columnSortHandlerByName);

		// We know that the data is sorted alphabetically by default.
		table.getColumnSortList().push(achatColumn);

		ListHandler<Achat> columnSortHandlerByComment = new ListHandler<Achat>(
				list);
		columnSortHandlerByComment.setComparator(commentColumn,
				new Comparator<Achat>() {
					public int compare(Achat o1, Achat o2) {
						if (o1 == o2) {
							return 0;
						}

						// Compare the name columns.
						if (o1 != null) {
							return (o2 != null) ? o1.comment
									.compareTo(o2.comment) : 1;
						}
						return -1;
					}
				});

		table.addColumnSortHandler(columnSortHandlerByComment);

		return table;
	}

	private void setListAchat() {
		List<Achat> list = this.dataProvider.getList();
		list.clear();
		for (Achat achat : AchatFactory.getListAchat(this.contactInfo)) {
			list.add(achat);
		}
	}

}
